package com.queue.cli.initializer;

import com.queue.cli.handler.SubscriberHandler;
import com.queue.common.protocol.MessageDecoder;
import com.queue.common.protocol.MessageEncoder;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;

public class SubscriberInitializer extends ChannelInitializer<SocketChannel> {
    //订阅者消息处理器
    private SubscriberHandler subscriberHandler;

    public SubscriberInitializer(SubscriberHandler subscriberHandler) {
        this.subscriberHandler = subscriberHandler;
    }

    /**
     * 该方法主要是为客户端channel设置编解码器以及消息处理器
     * @param channel netty通道
     * @throws Exception
     */
    @Override
    protected void initChannel(SocketChannel channel) throws Exception {
        ChannelPipeline pipeline = channel.pipeline();
        pipeline.addLast("encoder",new MessageEncoder());
        pipeline.addLast("decoder",new MessageDecoder());
        pipeline.addLast(subscriberHandler);
    }
}
